package com.mobile.web.messageNotification;

import com.mobile.service.dto.MessageNotificationDTO;
import com.mobile.service.dto.MessageNotificationQueryDTO;
import com.mobile.service.inter.MessageNotificationService;
import com.mobile.util.*;
import com.mobile.web.base.BaseController;
import com.mobile.util.config.aop.AutoFill;
import com.mobile.util.config.aop.OperateType;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 消息通知表API接口
 */
@RestController
@RequestMapping("/messageNotification")
@Tag(name = "消息通知管理接口", description = "包含消息通知的查询、新增、修改、删除等操作")
public class MessageNotificationController extends BaseController {

    @Autowired
    private MessageNotificationService messageNotificationService;

    @PostMapping("/addMessageNotification")
    //@RequiresPermissions("messageNotificationmana:add:1")
    @Operation(summary = "创建消息通知")
    @AutoFill(OperateType.INSERT)
    public Object addMessageNotification(@Valid MessageNotificationDTO dto) {
        Map<String, Object> map;
        try {
            map = messageNotificationService.addMessageNotification(dto);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping(value = "/getMessageNotificationListPage")
    //@RequiresPermissions("messageNotificationmana:read:1")
    @Operation(summary = "分页查询消息通知")
    public Object getMessageNotificationListPage(@Validated MessageNotificationQueryDTO queryDTO) {
        Map<String, Object> map = new HashMap<>();
        try {
            PageResult<MessageNotificationDTO> page = messageNotificationService.getMessageNotificationListPage(queryDTO);
            map.put("page", page);
            map.put("data", page.getPageDatas());
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping(value = "/getMessageNotificationById")
    //@RequiresPermissions("messageNotificationmana:edit:1")
    @Operation(summary = "查询消息通知详情")
    public Object getMessageNotificationById(
        @Parameter(description = "消息通知主键", required = true) 
        @RequestParam("notificationId") Integer id) {
        Map<String, Object> map = new HashMap<>();
        try {
            MessageNotificationDTO dto = messageNotificationService.getMessageNotificationById(id);
            if (StringUtil.isEmpty(dto)) {
                map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
                map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0001"));
            } else {
                map.put("data", dto);
                map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
            }
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @PostMapping("/updateMessageNotification")
    //@RequiresPermissions("MessageNotificationmana:edit:1")
    @Operation(summary = "编辑消息通知")
    @AutoFill(OperateType.UPDATE)
    public Object updateMessageNotification(@Valid @RequestBody MessageNotificationDTO dto) {
        Map<String, Object> map;
        try {
            map = messageNotificationService.updateMessageNotification(dto);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping("/removeMessageNotification")
    //@RequiresPermissions("messageNotificationmana:delete:1")
    public Object removeMessageNotification(@RequestParam("notificationId") Integer id) {
        Map<String, Object> map;
        try {
            map = messageNotificationService.removeMessageNotification(id);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping("/getMessageNotificationList")
    //@RequiresPermissions("messageNotificationmana:read:1")
    @Operation(summary = "条件查询消息通知列表")
    public Object getMessageNotificationList(@Validated MessageNotificationQueryDTO queryDTO) {
        Map<String, Object> map = new HashMap<>();
        try {
            List<MessageNotificationDTO> list = messageNotificationService.getMessageNotificationList(queryDTO);
            map.put("data", list);
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }
}
